package Weapon

import (
	codecs "sanguo/codec/cs"
	"sanguo/node/common/GlobalConstData"
	"sanguo/node/common/transaction"
	"sanguo/node/node_game"
	"sanguo/node/node_game/module/role"
	"sanguo/node/node_game/user"
	table_Weapon "sanguo/node/table/excel/table/Weapon"
	cs_message "sanguo/protocol/cs/message"
	"time"

	"github.com/golang/protobuf/proto"
)

type transactionWeaponUpStar struct {
	transaction.TransactionBase
	user    *user.User
	req     *codecs.Message
	resp    *cs_message.WeaponUpStarToC
	errcode cs_message.EnumType
}

func (this *transactionWeaponUpStar) GetModuleName() string {
	return "Weapon"
}

func (this *transactionWeaponUpStar) GetReplyMsg() proto.Message {
	return &cs_message.DemountWeaponToC{}
}

func (this *transactionWeaponUpStar) SetExpiredTime(expired time.Time) {
	this.Expired = expired
}

func (this *transactionWeaponUpStar) GetExpiredTime() time.Time {
	return this.Expired
}

func (this *transactionWeaponUpStar) Begin() {

	this.resp = &cs_message.WeaponUpStarToC{}
	msg := this.req.GetData().(*cs_message.WeaponUpStarToS)

	roleModule := this.user.GetModuleByName("role").(*role.RoleModule)
	role := roleModule.CurrentRole()
	if role == nil || !role.IsRoleModleData() {
		node_game.Infoln(role.GetUserID(), "WeaponUpStar fail: role is nil")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}

	weapon := role.GetRoleWeapon().GetWeapon(msg.GetWeaponId())
	if weapon != nil {
		node_game.Infof("%s %d Call WeaponUpStar wid:%d", role.GetUserID(), role.GetID(), msg.GetWeaponId())
		if weapon.WeaponStar >= GlobalConstData.StarMaxLevel {
			node_game.Infoln(role.GetUserID(), role.GetID(), "WeaponUpStar fail:Weapon Star is Max ")
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}

		//武器升星
		costShard := weapon.GetWeaponCostShard()

		if costShard == 0 {
			node_game.Infoln(role.GetUserID(), role.GetID(), "WeaponUpStar fail:Get WeaponShard == 0")
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}

		if !role.GetRoleWeaponShard().UseWeaponShard(msg.GetWeaponId(), costShard) {
			node_game.Infoln(role.GetUserID(), role.GetID(), "WeaponUpStar fail:UseWeaponShard is fail")
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}

		role.GetRoleWeapon().WeaponUpStar(weapon)

		node_game.Infoln(role.GetUserID(), role.GetID(), "WeaponUpStar OK")
		this.errcode = cs_message.EnumType_OK
		this.user.EndTrans(this)
	} else {
		node_game.Infof("%s %d Call WeaponActivate wid:%d", role.GetUserID(), role.GetID(), msg.GetWeaponId())
		//武器激活
		weaponDef := table_Weapon.Table_.IDMap[msg.GetWeaponId()]
		if weaponDef == nil {
			node_game.Infoln(role.GetUserID(), role.GetID(), "WeaponActivate fail:weaponDef is nil")
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}

		if !role.GetRoleWeaponShard().UseWeaponShard(msg.GetWeaponId(), weaponDef.ActivateShards) {
			node_game.Infoln(role.GetUserID(), role.GetID(), "WeaponActivate fail:UseWeaponShard is fail")
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}

		weapons := make(map[int32]int32)
		weapons[msg.GetWeaponId()] = int32(1)
		role.AddWeapon(weapons)

		node_game.Infoln(role.GetUserID(), role.GetID(), "WeaponActivate OK")
		this.errcode = cs_message.EnumType_OK
		this.user.EndTrans(this)
	}

	//重新计算战斗属性
	role.TotalBattleAttr()

	//发送背包变化
	role.OnWeaponDirty()
	role.OnWeaponShardDirty()
}

func (this *transactionWeaponUpStar) End() {
	this.resp.Errcode = cs_message.EnumType(this.errcode).Enum()
	this.user.Reply(this.req.GetSeriNo(), this.resp)
}

func (this *transactionWeaponUpStar) Timeout() {
	this.resp.Errcode = cs_message.EnumType(cs_message.EnumType_RETRY).Enum()
	this.user.EndTrans(this)
}

func init() {
	user.RegisterTransFunc(&cs_message.WeaponUpStarToS{}, func(user *user.User, msg *codecs.Message) transaction.Transaction {
		return &transactionWeaponUpStar{
			user: user,
			req:  msg,
		}
	})
}
